import {
  AddArticleCommentResponse,
  ArticleCommentResponse,
  ArticleDetailResponse,
} from '@/types/data'
import { RootThunkAction } from '@/types/store'
import http from '@/utils/http'
import dayjs from 'dayjs'
import localizedFormat from 'dayjs/plugin/localizedFormat'
dayjs.extend(localizedFormat)

export const getArticleById = (id: string): RootThunkAction => {
  return async (dispatch) => {
    const res = await http.get<ArticleDetailResponse>(`/articles/${id}`)
    dispatch({
      type: 'article/getArticleById',
      payload: {
        ...res.data.data,
        pubdate: dayjs(res.data.data.pubdate).format('LL'),
      },
    })
  }
}

export const followAuthor = (
  authorId: string,
  isFollowed: boolean
): RootThunkAction => {
  return async (dispatch) => {
    console.log(authorId, isFollowed)
    if (isFollowed) {
      await http.delete(`/user/followings/${authorId}`)
    } else {
      await http.post('/user/followings', {
        target: authorId,
      })
    }

    dispatch({
      type: 'article/update',
      payload: {
        name: 'is_followed',
        value: !isFollowed,
      },
    })
  }
}

export const collectedArticle = (
  art_id: string,
  is_collected: boolean
): RootThunkAction => {
  return async (dispatch) => {
    if (is_collected) {
      await http.delete(`/article/collections/${art_id}`)
    } else {
      await http.post('/article/collections', {
        target: art_id,
      })
    }

    dispatch({
      type: 'article/update',
      payload: {
        name: 'is_collected',
        value: !is_collected,
      },
    })
  }
}

export const likeArticle = (
  art_id: string,
  attitude: number
): RootThunkAction => {
  return async (dispatch) => {
    if (attitude === 1) {
      await http.delete(`/article/likings/${art_id}`)
    } else {
      await http.post('/article/likings', {
        target: art_id,
      })
    }

    dispatch({
      type: 'article/update',
      payload: {
        name: 'attitude',
        value: attitude === 1 ? 0 : 1,
      },
    })
  }
}

export const getArticleCommentsFirst = (
  type: string,
  targetId: string,
  offset: string | null
): RootThunkAction => {
  return async (dispatch) => {
    const res = await http.get<ArticleCommentResponse>('/comments', {
      params: {
        type,
        source: targetId,
        offset,
      },
    })

    dispatch({
      type: 'article/getArticleCommentsFirst',
      payload: res.data.data,
    })
  }
}

export const getArticleComments = (
  type: string,
  targetId: string,
  offset: string | null
): RootThunkAction => {
  return async (dispatch) => {
    const res = await http.get<ArticleCommentResponse>('/comments', {
      params: {
        type,
        source: targetId,
        offset,
      },
    })

    dispatch({ type: 'article/getArticleComments', payload: res.data.data })
  }
}

export const addArticleComment = (
  art_id: string,
  value: string
): RootThunkAction => {
  return async (dispatch) => {
    const res = await http.post<AddArticleCommentResponse>('/comments', {
      target: art_id,
      content: value,
    })

    dispatch({
      type: 'article/addArticleComment',
      payload: res.data.data.new_obj,
    })
  }
}

export const articleCommentThumbUp = (
  com_id: string,
  is_liking: boolean
): RootThunkAction => {
  return async (dispatch) => {
    if (!is_liking) {
      await http.post('/comment/likings', {
        target: com_id,
      })
    } else {
      await http.delete(`/comment/likings/${com_id}`)
    }

    dispatch({
      type: 'article/articleCommentThumbUp',
      payload: {
        id: com_id,
        is_liking: !is_liking,
      },
    })
  }
}

export const updataCommentCount = (
  commentId: string,
  total: number
): RootThunkAction => {
  return (dispatch) => {
    dispatch({
      type: 'article/updataCommentCount',
      payload: {
        commentId,
        total,
      },
    })
  }
}
